package com.zhongge.operations;

import com.zhongge.book.Book;
import com.zhongge.book.BookList;

import java.util.Scanner;

@SuppressWarnings("all")
/**
 * @ClassName AddBook
 * @Description TODO 新增图书
 * @Author zhongge
 * @Date 2025/7/14 19:29
 * @Version 1.0
 */
public class AddBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scanner.nextLine();
        System.out.println("请输入作者");
        String author = scanner.nextLine();
        System.out.println("请输入价格");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入类型");
        String type = scanner.nextLine();

        //创建图书
        Book book = new Book(name, price, author, type);

        //判断是否满了
        if (bookList.getUsedSize() == bookList.getBookArr().length) {
            //扩容
            bookList.setBookArr(bookList.getUsedSize() * 2);
        }

        //判断此书是不是已经有了
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if (name.equals(bookList.getBook(i).getName())) {
                System.out.println("插入失败，此书已存在");
                return;
            }
        }

        //增加
        bookList.getBookArr()[bookList.getUsedSize()] = book;
        bookList.setUsedSize(bookList.getUsedSize() + 1);
        System.out.println("增加成功！！！");
    }
}
